package com.vision.mapper;

import com.vision.pojo.entity.Video;
import com.vision.pojo.vo.*;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Service;

import java.util.List;

@Mapper
public interface VideoMapper {
    /**
     * 添加视频
     * @param video 视频
     */
    @Insert("insert into vs_video (user_id,title,description,video_url,cover_url,upload_time,update_time,duration) values (#{userId},#{title},#{description},#{videoUrl},#{coverUrl},NOW(),NOW(),#{duration})")
    void insertVideo(Video video);

    /**
     * 删除视频
     * @param videoId 视频id
     */
    @Delete("delete from vs_video where id = #{videoId}")
    void deleteVideo(Integer videoId);

    /**
     * 修改视频状态为审核通过
     * @param state 视频状态
     * @param reviewProcess 审核进度
     * @param videoId 视频id
     */
    @Update("update vs_video set vs_video.update_time = now(),state = #{state},review_process = #{reviewProcess} where id = #{videoId}")
    void updateReviewState(Integer state, Integer reviewProcess , Integer videoId);

    /**
     * 修改视频
     * @param video 视频
     */
    @Update("update vs_video set title = #{title}, cover_url = #{coverUrl} ,description = #{description},update_time = now() where id = #{id}")
    void updateVideo(Video video);

    /**
     * 分页查询视频
     * @param userId 用户id
     * @param state 视频状态
     * @return 视频列表
     */
    @Select("select * from vs_video where user_id = #{userId} and state = #{state}")
    List<Video> getVideoForPage(Integer userId, Integer state);

    /**
     * 查询当前用户视频中 state 为 2 的视频 ID
     * @param userId 用户id
     * @return 视频id
     */
    @Select("select id from vs_video where state = 2 and user_id = #{userId} limit 1")
    Integer selectVideoIdByState (Integer userId);

    /**
     * 查询当前用户视频中 state 为 2 (处于审核中)的视频
     * @param userId 用户id
     * @return 视频
     */
    @Select("select * from vs_video where state = 2 and user_id = #{userId} limit 1")
    Video selectVideoByStateAndUserId(Integer userId);

    /**
     * 查询所有处于审核中的视频
     * @return 视频列表
     */
    List<ReviewVideoVO> selectAllUnderReviewVideo();

    /**
     * 分页查询视频列表
     * @return 视频列表
     */
    List<VideoAndUserVO> selectVideoForPageOrderByViewCount(String title);

    /**
     * 分页查询视频列表
     * @return 视频列表
     */
    List<VideoAndUserVO> selectVideoForPageByTagId(Integer[] tagIds);

    /**
     * 根据视频id查询视频
     * @param videoId 视频id
     * @return 视频
     */
    @Select("select * from vs_video where id = #{videoId}")
    Video selectVideoById(Integer videoId);

    /**
     * 增加视频播放量
     * @param videoId 视频id
     */
    @Update("update vs_video set views = views + 1 where id = #{videoId}")
    void updateVideoViewCountByVideoId(Integer videoId);

    /**
     * 查询所有视频
     * @return 视频列表
     */
    @Select("select * from vs_video where state = 1")
    List<Video> selectAllVideos();
}
